package com.zhoug.mpchart.markers;

import android.content.Context;
import android.widget.TextView;

import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.zhoug.mpchart.MPChartConfig;
import com.zhoug.mpchart.R;
import com.zhoug.mpchart.utils.MPChartUtils;
import com.zhoug.mpchart.views.MPIconView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 雷达图marker:支持单个,堆叠,并排
 *
 * @Author 35574
 * @Date 2022/1/5
 * @Description
 */
public class MPRadarMarker extends MPBaseMarker {
    private static final String TAG = ">>>BarMarker";
    private static final boolean DEBUG = MPChartConfig.DEBUG;
    public static final int SHOW_TYPE_HIGHLIGHT = 1;//只显示点击的高亮的数据
    public static final int SHOW_TYPE_All = 2;// 全部数据

    /**
     * {@link #SHOW_TYPE_HIGHLIGHT,#SHOW_TYPE_All ,}
     */
    protected int mShowType = SHOW_TYPE_All;//
    protected boolean mShowSum = false;//是否在label后面显示总数
    private String mLabel;
    /**
     * {@link MPIconView#CIRCLE, MPIconView#RING}
     */
    private int mIcon = MPIconView.NULL;

    protected OnBindDataListener mOnBindDataListener;

    private TextView mTvXValue;
    private TextView mTvSumValue;
    private MPIconView mIvXValueIcon;
    private RecyclerView mRecyclerView;
    private Adapter mAdapter;


    public MPRadarMarker(Context context) {
        this(context, R.layout.mp_list_marker);
        mIvXValueIcon = findViewById(R.id.iv_xValueIcon);
        mTvXValue = findViewById(R.id.tv_xValue);
        mTvSumValue = findViewById(R.id.tv_sumValue);
        mRecyclerView = findViewById(R.id.recyclerView);
        mAdapter = new Adapter();
        mAdapter.setData(new ArrayList<>());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        mRecyclerView.setAdapter(mAdapter);
    }

    public MPRadarMarker(Context context, int layoutResource) {
        super(context, layoutResource);
    }

    @Override
    public void onBindView(Entry entryForHighlight, Highlight highlight, @NonNull MPMarkerData markerData) {
        List<MPMarkerData.YData> list = null;
        float sum = 0;
        List<MPMarkerData.YData> yData = markerData.getData();
        if (yData != null && yData.size() > 0) {
            sum = yData.get(0).getSelfSum();
            if (mShowType == SHOW_TYPE_All) {
                list = yData;
            } else {
                list = new ArrayList<>();
                for (MPMarkerData.YData item : yData) {
                    if (item.getEntryIndex() == markerData.getEntryIndex()) {
                        list.add(item);
                        break;
                    }
                }
            }
        }



        List<MPMarkerData.YData> data = mAdapter.getData();
        data.clear();
        if (list != null && list.size() > 0) {
            data.addAll(list);
        }
        calculateMaxLabelLength(data,mLabel);
        if (mShowSum && mLabel != null) {
            if (mOnBindDataListener == null || !mOnBindDataListener.onBindXLabel(mTvXValue, mLabel, mShowSum, sum)) {
                mTvXValue.setText(getLabelFormat(mLabel));
                mTvSumValue.setText(getFormattedValue(sum));
            }
            mTvXValue.setVisibility(VISIBLE);
            mTvSumValue.setVisibility(VISIBLE);
            if(mIcon== MPIconView.NULL){
                mIvXValueIcon.setVisibility(GONE);
            }else{
                mIvXValueIcon.setVisibility(INVISIBLE);
            }
        } else {
            mTvXValue.setVisibility(GONE);
            mTvSumValue.setVisibility(GONE);
            mIvXValueIcon.setVisibility(GONE);
        }
        mAdapter.notifyDataSetChanged();
    }


    public class Adapter extends MPBaseRecyclerViewAdapter<MPMarkerData.YData> {
        public Adapter() {
        }

        @Override
        protected int getLayoutId(int viewType) {
            if (mOnBindDataListener != null && mOnBindDataListener.getListItemLayoutId() != 0) {
                return mOnBindDataListener.getListItemLayoutId();
            }
            return R.layout.mp_marker_item;
        }

        @Override
        protected void onBindData(MPBaseViewHolder holder, MPMarkerData.YData data, int position, int viewType) {
            if (data != null) {
                MPIconView mpIcon = holder.getView(R.id.iv_icon);
                TextView tvLabel = holder.getView(R.id.tv_label);
                TextView tvValue = holder.getView(R.id.tv_value);
                if(mIcon== MPIconView.NULL){
                    mpIcon.setVisibility(GONE);
                }else{
                    mpIcon.setVisibility(VISIBLE);
                }
                if (mOnBindDataListener == null || !mOnBindDataListener.onBindItemData(holder, data, position, viewType)) {
                    mpIcon.setColor(data.getColor());
                    mpIcon.setShape(mIcon);
                    tvLabel.setText(getLabelFormat(data.getLabel()));
                    if (mShowPercent) {
                        tvValue.setText(MPChartUtils.formatString("%s  (%s%%)", getFormattedValue(data.getValue()), getPercentValue(data.getSelfRatio())));
                    } else {
                        tvValue.setText(getFormattedValue(data.getValue()));
                    }
                }
                mpIcon.postInvalidate();
            }
        }

    }

    /**
     * {@link #SHOW_TYPE_HIGHLIGHT,#SHOW_TYPE_All ,}
     */
    public void setShowType(int showType) {
        this.mShowType = showType;
    }

    public void showSum(String label, boolean showSum) {
        this.mLabel = label;
        this.mShowSum = showSum;
    }


    public void setIcon(int icon) {
        this.mIcon = icon;
    }

    public void setOnBindDataListener(OnBindDataListener onBindDataListener) {
        mOnBindDataListener = onBindDataListener;
    }

    public interface OnBindDataListener {
        /**
         * x轴label数据绑定
         *
         * @param tvXLabel
         * @param xLabel
         * @param showSum
         * @param sum
         * @return false 使用默认的
         */
        boolean onBindXLabel(TextView tvXLabel, String xLabel, boolean showSum, float sum);

        /**
         * listItem布局文件
         *
         * @return
         */
        @LayoutRes
        int getListItemLayoutId();

        /**
         * listItem布局文件数据绑定
         *
         * @param holder
         * @param data
         * @param position
         * @param viewType
         */
        boolean onBindItemData(MPBaseViewHolder holder, MPMarkerData.YData data, int position, int viewType);
    }

}
